package net.xo68.destiny.starter.manager.worker.route.impl;

import lombok.extern.slf4j.Slf4j;
import net.xo68.destiny.starter.manager.worker.route.IWorkerRouteStrategy;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 随机路由策略实现
 * @author wuxie
 * @version 2020-12-23
 */
@Slf4j
@Component("randomWorkerRouteStrategy")
public class RandomWorkerRouteStrategy  implements IWorkerRouteStrategy {

    @Override
    public String exec(List<String> hosts) {
        if(hosts==null || hosts.isEmpty()){
            return null;
        }
        int idx= RandomUtils.nextInt(0,hosts.size());
        return hosts.get(idx);
    }
}
